package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
	@Autowired
	private ShoppingCartService shoppingCartService;

	@Autowired
	private UserService userService;

	@Autowired
	private AddressBookService addressBookService;

	@Autowired
	private OrderDetailService orderDetailService;

	@Override
	public void submit(Orders orders){
		Long userId = BaseContext.getCurrentId();

		User user = userService.getById(userId);

		AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
		if(addressBook == null){
			throw new CustomException("用户地址信息有误，不能下单");
		}
		LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ShoppingCart::getUserId,userId);
		List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);

		long orderId = IdWorker.getId();//订单号

		AtomicInteger amount = new AtomicInteger(0);

		List<OrderDetail> orderDetails = shoppingCarts.stream().map(shoppingCart -> {
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setName(shoppingCart.getName());
			orderDetail.setImage(shoppingCart.getImage());
			orderDetail.setOrderId(orderId);
			orderDetail.setDishId(shoppingCart.getDishId());
			orderDetail.setSetmealId(shoppingCart.getSetmealId());
			orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
			orderDetail.setNumber(shoppingCart.getNumber());
			orderDetail.setAmount(shoppingCart.getAmount());
			amount.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
			return orderDetail;
		}).collect(Collectors.toList());

		orderDetailService.saveBatch(orderDetails);

		orders.setId(orderId);
		orders.setOrderTime(LocalDateTime.now());
		orders.setCheckoutTime(LocalDateTime.now());
		orders.setStatus(2);
		orders.setAmount(new BigDecimal(amount.get()));//总金额
		orders.setUserId(userId);
		orders.setAddressBookId(addressBook.getId());
		orders.setNumber(String.valueOf(orderId));
		orders.setUserName(user.getName());
		orders.setPayMethod(1);
		orders.setConsignee(addressBook.getConsignee());
		orders.setPhone(addressBook.getPhone());
		orders.setAddress(addressBook.getDetail());
		orders.setUserName(user.getName());

		super.save(orders);

		shoppingCartService.remove(queryWrapper);
	}


}